import { Injectable } from '@angular/core'
import { OnInit, OnDestroy } from '@angular/core'
import { ICardRouteOfStore } from '@shared/interfaces/store'
import { IBaseInfoColumn } from '@shared/interfaces/baseInfo'
import { EPaymentBrand, ECustomAcl } from '@shared/enums'
import { CustomACLService } from '@shared/services/acl.service'
import { PageService } from '@shared/services/page.service'
import { I18NService } from '@core'

@Injectable()
export class TabCardChannelsService implements OnInit, OnDestroy {
  constructor(
    private pageService: PageService,
    private customACLService: CustomACLService,
    private i18n: I18NService,
  ) {}

  ngOnInit(): void {
    this.initialData()
  }

  ngOnDestroy(): void {
    this.customACLService.removeACLAbility(ECustomAcl.StoreEditable)
    this.customACLService.removeACLAbility(ECustomAcl.VisaCurrencyChannel)
  }

  initialData() {
    const isStoreLocked = this.pageService.isStoreLocked
    const isStoreDisplayInTask = this.pageService.isStoreDisplayInTask
    if (!isStoreDisplayInTask && !isStoreLocked) {
      this.customACLService.addACLAbility(ECustomAcl.StoreEditable)
    }
  }

  getChannelColumns(channel: ICardRouteOfStore): IBaseInfoColumn[][] {
    switch (channel.paymentBrand) {
      case EPaymentBrand.Visa:
        return this.getVisaChannelColumns(channel)
      case EPaymentBrand.MasterCard:
        return this.getMasterCardChannelColumns(channel)
      case EPaymentBrand.AME:
        return this.getAmexChannelColumns(channel)
      case EPaymentBrand.UPI:
        return this.getUpiChannelColumns(channel)
      default:
        return []
    }
  }

  private getVisaChannelColumns(channel: ICardRouteOfStore): IBaseInfoColumn[][] {
    if (channel.currency) {
      this.customACLService.addACLAbility(ECustomAcl.VisaCurrencyChannel)
    }
    return [
      [
        {
          span: 12,
          label: 'Institution Code in Offline System',
          value: channel.chanMerParams.offlineInsCode,
        },
        {
          span: 12,
          label: 'Visa BIN',
          value: channel.chanMerParams.bin,
        },
      ],
      [
        {
          span: 6,
          label: 'Visa MCC',
          value: channel.mcc as string,
        },
        {
          span: 6,
          label: 'Transaction Fee',
          value: this.toPercentage(channel.fee),
        },
        {
          span: 6,
          label: 'Channel transaction currency',
          value: channel.currency,
        },
        {
          span: 6,
          label: 'OPERATION',
          labelI18n: 'GENERAL.INFO.OPERATION',
          operate: true,
          acl: ['api.put.turtle.store.one.channel', ECustomAcl.StoreEditable],
          aclMode: 'allOf',
        },
      ],
    ]
  }

  private getMasterCardChannelColumns(channel: ICardRouteOfStore): IBaseInfoColumn[][] {
    return [
      [
        {
          span: 8,
          label: 'Institution Code in Offline System',
          value: channel.chanMerParams.offlineInsCode,
        },
        {
          span: 8,
          label: 'MasterCard BIN',
          value: channel.chanMerParams.bin,
        },
        {
          span: 8,
          label: 'MasterCard ICA',
          value: channel.chanMerParams.ica,
        },
      ],
      [
        {
          span: 6,
          label: 'MasterCard MCC',
          value: channel.mcc as string,
        },
        {
          span: 6,
          label: 'Transaction Fee',
          value: this.toPercentage(channel.fee),
        },
        {
          span: 6,
          label: 'Channel transaction currency',
          value: channel.currency,
        },
        {
          span: 6,
          label: 'OPERATION',
          labelI18n: 'GENERAL.INFO.OPERATION',
          operate: true,
          acl: ['api.put.zebra.store.one.channel', ECustomAcl.StoreEditable],
          aclMode: 'allOf',
        },
      ],
    ]
  }

  private getAmexChannelColumns(channel: ICardRouteOfStore): IBaseInfoColumn[][] {
    return [
      [
        {
          span: 8,
          label: 'AIN',
          value: channel.chanMerParams.ain,
        },
        {
          span: 8,
          label: 'First 3 digits of S/E Number',
          value: channel.chanMerParams.first3DigitsOfSENumber,
        },
        {
          span: 8,
          label: 'Store English Name',
          value: channel.chanNameEN,
        },
      ],
      [
        {
          span: 8,
          label: 'Store English Address',
          value: channel.chanAddrEN,
        },
        {
          span: 8,
          label: 'American Express MCC',
          value: channel.mcc as string,
        },
        {
          span: 8,
          label: 'Transaction Fee',
          value: this.toPercentage(channel.fee),
        },
      ],
      [
        {
          span: 8,
          label: 'Channel transaction currency',
          value: channel.currency,
        },
        {
          span: 8,
          label: 'S/E Number',
          value: channel.sponsorMerCode,
        },
        {
          span: 8,
          label: 'OPERATION',
          labelI18n: 'GENERAL.INFO.OPERATION',
          operate: true,
          acl: ['api.put.zebra.store.one.channel', ECustomAcl.StoreEditable],
          aclMode: 'allOf',
        },
      ],
    ]
  }

  private getUpiChannelColumns(channel: ICardRouteOfStore): IBaseInfoColumn[][] {
    return [
      [
        {
          span: 12,
          labelI18n: 'GENERAL.INFO.CHANNEL.UPI_ACQUIRING_IIN',
          label: 'ain',
          value: channel.chanMerParams.ain,
        },
        {
          span: 12,
          labelI18n: 'GENERAL.INFO.CHANNEL.UPI_FOWARDING_IIN',
          label: 'fin',
          value: channel.chanMerParams.fin,
        },
      ],
      [
        {
          span: 6,
          label: 'UPI MCC',
          value: channel.mcc as string,
        },
        {
          span: 6,
          labelI18n: 'GENERAL.INFO.TRANSACTION_FEE',
          label: 'fee',
          value: this.toPercentage(channel.fee),
        },
        {
          span: 6,
          labelI18n: 'CARD.GENERAL.INFO.TRANSACTION_CURRENCY',
          label: 'currency',
          value: channel.currency,
        },
        {
          span: 6,
          label: 'OPERATION',
          labelI18n: 'GENERAL.INFO.OPERATION',
          operate: true,
          acl: ['api.put.zebra.store.one.channel', ECustomAcl.StoreEditable],
          aclMode: 'allOf',
        },
      ],
    ]
  }

  private toPercentage(fee: number): string {
    const per = fee * 100
    return `${per.toFixed(4)}%`
  }

  private trans(str: string, params?: Object): string {
    return this.i18n.fanyi(str, params)
  }
}
